﻿using HarmonyLib;
using RemoteAdmin;
using RgCore.Modules;
using System.Reflection;
using CustomItem.Commands.CustomItemCommands;
using RgCore.Modules.Commands.SubPluginManagerCommands;

namespace CustomItem
{
    public class CustomItemPlugin : RgPlugin
    {
        public override string Name => "自定义物品";
        public override string Id => "CustomItem";
        public override bool CanUnload => false;
        public Harmony HarmonyPatch { get; private set; }
        public static CustomItemPlugin Instance { get; private set; }
        public SubPluginManager<ItemInfoAttribute> Manager { get; private set; }
        public SubPluginManagerCommand<ItemInfoAttribute> Command { get; private set; }

        public override void OnEnabled()
        {
            Instance = this;
            Manager = new SubPluginManager<ItemInfoAttribute>(Name, "CustomItems", typeof(CustomItem));
            Manager.LoadObjects();
            Command = new SubPluginManagerCommand<ItemInfoAttribute>("im", Manager);
            CommandProcessor.RemoteAdminCommandHandler.RegisterCommand(Command);
            CommandProcessor.RemoteAdminCommandHandler.RegisterCommand(new CustomItemCommand());
            CustomItem.SetEnabled(true);
            HarmonyPatch = new Harmony(Id);
            HarmonyPatch.PatchAll(Assembly.GetExecutingAssembly());
        }

        public override void OnDisabled()
        {
            CustomItem.SetEnabled(false);
            Instance = null;
            HarmonyPatch.UnpatchAll(Id);
            foreach (var type in Manager.AllTypes)
            {
                Manager.DisableObject(type.Key.Name, false);
            }
            CommandProcessor.RemoteAdminCommandHandler.UnregisterCommand(Command);
        }
    }
}